package my.models

import LoadingStatus
import formatTime
import models.Component
import models.Computer
import models.LocationUi
import models.User

data class MyObjectsViewState(
    val loadingStatus: LoadingStatus = LoadingStatus.Idle,
    val computers: List<ComputerUi> = emptyList(),
    val selectedObject: MyObject? = null,
    val components: List<ComponentUi> = emptyList(),
)

interface MyObject {
    val id: Int
    val serialNumber: String
    val name: String
}

data class ComputerUi(
    override val id: Int,
    override val serialNumber: String,
    val model: String,
    val manufacturer: String,
    val purchaseDate: String,
    val location: LocationUi,
    val status: String,
    val notes: String,
    val createdBy: User,
    val verifiedBy: User,
    val lastVerifiedAt: String,
    val components: List<ComponentUi>
) : MyObject {
    override val name = "$manufacturer $model"

    companion object {
        fun fromComputer(computer: Computer): ComputerUi {
            return ComputerUi(
                id = computer.id,
                serialNumber = computer.serialNumber,
                model = computer.model,
                manufacturer = computer.manufacturer,
                purchaseDate = formatTime(computer.purchaseDate, "dd.MM.yyyy"),
                location = LocationUi.from(computer.location),
                status = computer.status,
                notes = computer.notes,
                createdBy = computer.createdBy,
                verifiedBy = computer.verifiedBy,
                lastVerifiedAt = formatTime(computer.lastVerifiedAt, "HH:mm, dd.MM.yyyy"),
                components = computer.components.map { ComponentUi.fromComponent(it) }
            )
        }
    }
}

data class ComponentUi(
    override val id: Int,
    override val name: String,
    override val serialNumber: String,
    val type: String,
    val specifications: String,
    val computerId: Int? = null,
    val purchaseDate: String,
    val location: LocationUi,
    val createdBy: User,
    val verifiedBy: User,
    val lastVerifiedAt: String
) : MyObject {
    companion object {
        fun fromComponent(component: Component): ComponentUi {
            return ComponentUi(
                id = component.id,
                name = component.name,
                type = component.type,
                specifications = component.specifications,
                computerId = component.computerId,
                purchaseDate = formatTime(component.purchaseDate, "dd.MM.yyyy"),
                location = LocationUi.from(component.location),
                serialNumber = component.serialNumber,
                createdBy = component.createdBy,
                verifiedBy = component.verifiedBy,
                lastVerifiedAt = formatTime(component.lastVerifiedAt, "HH:mm, dd.MM.yyyy")
            )
        }
    }
}
